<template>
<div>
        <div class="header">
        <div class="headleft"> 
            <breadcrumb/>
        </div>
        <div class="headright">
            <p class="sceen ml-16 mr-8 cursor" @click="changeSystem()">
                <el-icon class="iconstyle01 mr-8"><Link /></el-icon>
                <span class="sceen-text">一张图</span>
            </p>
            <el-divider direction="vertical" />
            <el-dropdown>
                <span class="el-dropdown-link">
                    <p class="username ml-8 mr-8">
                        <el-icon class="headerImg mr-8 iconstyle"><Avatar /></el-icon>
                        <span>{{nickname}}</span>
                    </p>
                    <el-icon class="el-icon--right">
                        <arrow-down />
                    </el-icon>
                </span>
                <template #dropdown>
                    <el-dropdown-menu>
                        <el-dropdown-item @click="changePassword">
                            <p class="username  cursor" @click="changePassword">
                                <i class="iconfont icon-icon-lock"></i>
                                <span>修改密码</span>
                            </p>
                        </el-dropdown-item>
                        <!-- <el-dropdown-item @click="lookinfo">查看个人信息</el-dropdown-item> -->
                        <el-dropdown-item @click="loginOut">
                            <p class="username cursor" @click="loginOut()">
                                <el-icon class="iconstyle mr-8"><SwitchButton /></el-icon>
                                <span>退出登录</span>
                            </p>
                        </el-dropdown-item>
                    </el-dropdown-menu>
                </template>
            </el-dropdown> 
        </div>
    </div>
    <!-- 修改密码 -->
    <div class="lz-dialog">
        <el-dialog
        ref="posswordRef"
        :model-value="dialogPasswordVisible"
        title="修改密码"
        :before-close="closePassword"
        width="800px"
        :close-on-click-modal ="false"
        :show-close = 'showButton'
        draggable
        >
            <div>
                <el-form
                :model="formPassword"
                :inline="true"
                label-width="160px"
                :rules="rules"
                ref="passwordform"
                require-asterisk-position="left"
                size="default"
                scroll-to-error="true"
            >
                <el-form-item  class=" mb-24" label="原始密码" prop="original" required>
                    <el-input
                        placeholder="请输入原始密码"
                        type="password"
                        v-model="formPassword.original"
                        show-password
                        maxlength="50"
                    />
                </el-form-item>
                <el-form-item class=" mb-24" label="新密码" prop="new" required>
                    <el-input
                        placeholder="请输入新密码"
                        type="password"
                        show-password
                        v-model="formPassword.new"
                        maxlength="50"
                    />
                </el-form-item>
                <el-form-item  class=" mb-24" label="确认密码" prop="confirm" required>
                    <el-input
                        placeholder="请输入确认密码"
                        type="password"
                        v-model="formPassword.confirm"
                        show-password
                        maxlength="50"
                    />
                </el-form-item>
            </el-form>
            </div>
            <template #footer>
                <span class="dialog-footer">
                    <el-button v-if="showButton" class="btn-mixins-clear-default" @click="closePassword"
                    >返回</el-button>
                <el-button class="btn-mixins-clear-default" @click="savePassword(passwordform)" type="primary"
                    >确定</el-button>
                
                </span>
        </template>
        </el-dialog>
    </div>

</div>
</template>
<script setup>
import { Link  } from "@element-plus/icons-vue";
import breadcrumb from "./breadcrumb.vue"
import { useRouter, useRoute } from "vue-router";
import { useStore } from 'vuex';
import { reactive, ref, getCurrentInstance ,onMounted} from "vue";
import store from '@/store'
import { ElNotification,ElMessage, ElMessageBox, } from "element-plus";
import { loginOut as exit, resetPasswords as resetPasswords} from '@/api/user'
    const {proxy} = getCurrentInstance();
    const passwordform = ref('');
    let dialogPasswordVisible  = ref(false);
    let dialogInfoVisible = ref(false);
    const showButton = ref(true)
    const router = useRouter();
    let nickname =  ref('');
    nickname.value = JSON.parse(localStorage.getItem('userData')).realName
    const getparmes = () => {
      // 获取当前路由实例参数
      if(router.currentRoute.value.query.isUpPassword == 'true'){
        dialogPasswordVisible.value = router.currentRoute.value.query.isUpPassword;
        showButton.value = !router.currentRoute.value.query.isUpPassword;
      }
    }
    onMounted(()=>{
        getparmes()
    })
    const passwordverifiers  = (rule,value,callback)=> { 
        console.log(value)
        // let pattern = /^(?=.*[a-zA-Z])(?=.*[0-9])(?=.*[@#$%^&+=]).{8,16}$/;
        let pattern = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@#$!%*?&.])[A-Za-z\d@#$!%*?&.]{8,16}$/;
        let pattern_ = /^[^\u4e00-\u9fa5]+$/;//校验中文
        if(!pattern.test(value)&&value){
            return callback(new Error('请输入新密码(长度在8-16之间，必须包含字母（大写和小写）、数字和特殊字符。)'))
        }else if(!pattern_.test(value)&&value){
            return callback(new Error('请输入新密码(长度在8-16之间，必须包含字母（大写和小写）、数字和特殊字符。)'))
        }else if(!value){
            return callback(new Error('请输入新密码(长度在8-16之间，必须包含字母（大写和小写）、数字和特殊字符。)'))
        }else{
            return callback()
        }
    }
    const passwordverifiers_  = (rule,value,callback)=> { 
        console.log(value)
        let pattern = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@#$!%*?&.])[A-Za-z\d@#$!%*?&.]{8,16}$/;
        let pattern_ = /^[^\u4e00-\u9fa5]+$/;//校验中文
        if(!pattern.test(value)&&value){
            return callback(new Error('请输入确认密码(长度在8-16之间，必须包含字母（大写和小写）、数字和特殊字符。)'))
        }else if(!pattern_.test(value)&&value){
            return callback(new Error('请输入确认密码(长度在8-16之间，必须包含字母（大写和小写）、数字和特殊字符。)'))
        }else if(!value){
            return callback(new Error('请输入确认密码(长度在8-16之间，必须包含字母（大写和小写）、数字和特殊字符。)'))
        }else if(value && value!=formPassword.new){
            return callback(new Error('两次输入的密码不一致'))
        }else{
            return callback()
        }
    }
    const rules = reactive({
        original:[{ required: true, message: '请输入原始密码(长度在8-16之间，必须包含字母（大写和小写）、数字和特殊字符。)', trigger: 'blur' },],
        new:[{ required: true,validator: passwordverifiers,  trigger: 'blur' },],
        confirm:[{ required: true,validator: passwordverifiers_, trigger: 'blur' },],
        
    })
     const userData = JSON.parse(localStorage.getItem('userData'))
    const formPassword = reactive({
        original:'',
        new:'',
        confirm:''
    })
    let info = ref({
    })
    const loginOut=()=>{
            ElMessageBox.confirm(
                '确认要退出登录吗?',
                '提示',
                {
                    confirmButtonText: '确认',
                    cancelButtonText: '取消',
                    type: 'warning',
                }
            )
                .then(() => {
                    exit(userData.username).then((res)=>{
                            if(res.code === 200){
                                    ElMessageBox.close(false)
                                    setTimeout(() => {
                                        store.dispatch('app/logout')
                                    },100)
                            }else {
                                ElNotification({
                                    title: '提示',
                                    message: res.message?res.message:'服务器异常',
                                    type: 'warning',
                                })
                                if(res.code === 100007 ||  res.code === 100008 ||  res.code === 100011){
                                        store.dispatch('app/logout')
                                }
                            }
                        })

                })
                .catch(() => {
                    ElMessageBox.close(false)
                    ElMessage({
                        type: 'info',
                        message: '取消退出',
                    })
                })

    }

    const changePassword = ()=>{
        dialogPasswordVisible.value = true;
    }
    const savePassword = (passwordform)=>{
        if(!passwordform) return
         passwordform.validate(async(valid) => {
            if(valid){
                let userData = JSON.parse(localStorage.getItem('userData'))
               let obj = {
                    "confirmPassword": formPassword.confirm,
                    "newPassword": formPassword.new,
                    "oldPassword":  formPassword.original,
                    "userName":userData.username
                }
                resetPasswords(obj).then((res)=>{
                    if(res.code === 200){
                        ElNotification({
                            title: '成功',
                            message: '修改密码成功',
                            type: 'success',
                        })
                        closePassword()
                    }else{
                        ElNotification({
                            title: '提示',
                            message: res.message?res.message:'服务器异常',
                            type: 'warning',
                        })
                        if(res.code === 100007 ||  res.code === 100008 ||  res.code === 100011){
                                store.dispatch('app/logout')
                            }
                    }
                })
               
            }else{
                return;
            }
        })
    }
    const closePassword = ()=>{
        passwordform.value.resetFields();
        dialogPasswordVisible.value = false;
    }
    const lookinfo = ()=>{
        findMyInformation(userData.username).then((res)=>{  
            if(res.code === 200){
                info.value = res.body
                dialogInfoVisible.value = true;
            }else{
                ElNotification({
                    title: '提示',
                    message: res.message?res.message:'服务器异常',
                    type: 'warning',
                })
                        if(res.code === 100007 ||  res.code === 100008 ||  res.code === 100011){
                                store.dispatch('app/logout')
                            }
            }
        })
    }
    const closeInfo = ()=>{
        dialogInfoVisible.value = false;
    }
    const changeSystem = () => {
        // router.replace('/check')
        let path ='http://'+ window.location.host+'/#/'+'screen'
        window.open(path);
    }
</script>

<style lang='less' scoped>
.lz-dialog {
          :deep(.el-form--inline .el-form-item){
              width: 100%;
          }
          :deep(.el-input){
                width: 100%;
            }
          :deep(.el-form-item--default .el-form-item__content){
            width: calc(100% - 160px);
        }
          :deep(.el-dialog__header) {
            border-bottom: 1px solid #cccccc;
            padding: 16px;
            margin-right: 0;
            display: flex;
            justify-content: space-between;
            align-items: center;
          }
          :deep(.el-dialog__body){
            padding-right:60px
          }
          :deep(.el-dialog__title) {
            color: #1C222C;
            font-size: 20px;
            width: 100px;
          }
          :deep(.el-dialog__headerbtn ){
            position: static;
            top: 0px;
            right: 0;
            padding: 0;
            width: auto;
            height: auto;
          }
}
.header{
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 16px;
    height: 59px;
}
.headerimg{
    width: 30px;
    height: 30px;
    border-radius: 50%;
    display: block;
    background-color: #333;
    float: left;
    margin-top: 12px;
}
:deep(.el-form-item--default .el-form-item__content){
            width: calc(100% - 80px);
        }
.showinfo {
    display: flex;
    margin: 16px 32px;
}
.showstyle {
  margin-right: 24px;
  width: 250px;
}
.name{
    float: right;
    line-height: 56px;
    // margin-left: 16px;
    font-weight: bolder;
}
.headright{
    // height: 100%;
    display: flex;
    align-items: center;
    .username{
        display: inline-block;
        // line-height: 60px;
        .headerImg{
            width: 24px;
            height: 24px;
            background: #2e83fc;
            border-radius: 100px;
            color: #ffffff !important;

        }
        .iconstyle{
            color: #A9AFB9;
        }
        span{
            font-size: 14px;
            color: #4B515B;
        }
    }
    // .el-icon{
    //     margin-top: 24px;
    // }
}
.headpig{
    width: 30px;
    height: 30px;
    border-radius: 50%;
    display: block;
    float: left;
    margin-top: 10px;
    margin: 16px;
}

.admin{
    float: right;
    line-height: 48px;
    position: relative;
}
.icon{
    float: left;
    line-height: 48px;
}
.el-dropdown-link{
    cursor: pointer;
    position: relative;
}
.icon-Frame4545{
    margin: 16px;
}
.sceen-text{
    color: #2E83FC;
    font-size: 14px;
}
.iconstyle01{
    color: #2E83FC;
}
</style>